<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="项目分类" prop="projectCatalog">
        <el-input v-model="queryParams.projectCatalog" placeholder="请输入项目分类" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目金额" prop="projectAmout">
        <el-input v-model="queryParams.projectAmout" placeholder="请输入项目金额" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属单位" prop="deptId">
        <el-select v-model="queryParams.deptId" placeholder="请选择单位" style="width: 100%;" @change="handleQuery()">
          <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-row :gutter="10" class="mb8 top-right-btn small-line-height">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['base:bsProject:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['base:bsProject:export']">导出</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table border stripe v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="所属单位名称" align="center" prop="deptName" />
      <el-table-column label="项目负责人" align="center" prop="projectLeader" />
      <!-- <el-table-column label="计划开始时间" align="center" prop="planBeginData" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planBeginData, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column>
<el-table-column label="计划结束时间" align="center" prop="planEndData" width="120">
  <template slot-scope="scope">
          <span>{{ parseTime(scope.row.planEndData, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column>
<el-table-column label="实际开始时间" align="center" prop="factBeginData" width="120">
  <template slot-scope="scope">
          <span>{{ parseTime(scope.row.factBeginData, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column>
<el-table-column label="实际开始时间" align="center" prop="factEndData" width="120">
  <template slot-scope="scope">
          <span>{{ parseTime(scope.row.factEndData, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column> -->
      <!-- <el-table-column label="项目状态" align="center" prop="projectStatus" width="120" /> -->
      <!-- <el-table-column label="项目审批状态" align="center" prop="approvalStatus" width="120">4
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_status" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button v-if="scope.row.approvalStatus != '00'" size="mini" type="text" icon="el-icon-view"
            @click="handleUpdate(scope.row, true)" v-hasPermi="['base:bsProject:edit']">详情</el-button>
          <el-button v-if="scope.row.approvalStatus == '00'" size="mini" type="text"
            @click="onApproval(scope.row, '11')" v-hasPermi="['base:bsProject:edit']">发起审批</el-button>
          <el-button v-if="scope.row.approvalStatus == '11'" size="mini" type="text"
            @click="onApproval(scope.row, '22')" v-hasPermi="['base:bsProject:edit']">审批</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['base:bsProject:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['base:bsProject:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改项目对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :disabled="isDisabled">
        <!-- <el-form-item label="项目分类" prop="projectCatalog">
          <el-input v-model="form.projectCatalog" placeholder="请输入项目分类" />
        </el-form-item> -->
        <el-row :gutter="32">
          <el-col :span="24">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目编码" prop="projectCode">
              <el-input v-model="form.projectCode" placeholder="请输入项目编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="24">
            <el-form-item label="所属单位" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择单位" style="width: 100%;" @change="setDeptName">
                <el-option v-for="item in deptOptions" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目负责人" prop="projectLeader">
              <el-input v-model="form.projectLeader" placeholder="请输入项目负责人" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="项目概况" prop="projectGeneral">
              <el-input v-model="form.projectGeneral" placeholder="请输入项目概况" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目规模" prop="projectSacle">
              <el-input v-model="form.projectSacle" placeholder="请输入项目规模" />
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="计划开始时间" prop="planBeginData">
              <el-date-picker clearable v-model="form.planBeginData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择计划开始时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划结束时间" prop="planEndData">
              <el-date-picker clearable v-model="form.planEndData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择计划结束时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="32">
          <el-col :span="12">
            <el-form-item label="实际开始时间" prop="factBeginData">
              <el-date-picker clearable v-model="form.factBeginData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择实际开始时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际开始时间" prop="factEndData">
              <el-date-picker clearable v-model="form.factEndData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择实际开始时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row> -->
        <!-- <el-form-item label="投标价" prop="bidPrice">
          <el-input v-model="form.bidPrice" placeholder="请输入投标价" />
        </el-form-item>
        <el-form-item label="控制价" prop="controlPrice">
          <el-input v-model="form.controlPrice" placeholder="请输入控制价" />
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="form.taxRate" placeholder="请输入税率" />
        </el-form-item>
        <el-form-item label="施工单位id" prop="shigongCompanyId">
          <el-input v-model="form.shigongCompanyId" placeholder="请输入施工单位id" />
        </el-form-item>
        <el-form-item label="监理单位id" prop="jianliCompanyId">
          <el-input v-model="form.jianliCompanyId" placeholder="请输入监理单位id" />
        </el-form-item>
        <el-form-item label="审计单位id" prop="shenjiCompanyId">
          <el-input v-model="form.shenjiCompanyId" placeholder="请输入审计单位id" />
        </el-form-item>
        <el-form-item label="地区" prop="address">
          <el-input v-model="form.address" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="是否延期 0否 1是" prop="postpone">
          <el-input v-model="form.postpone" placeholder="请输入是否延期 0否 1是" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!isDisabled" :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">{{ isDisabled ? '关 闭' : '取 消' }}</el-button>
      </div>
    </jy-dialog>
  </div>
</template>

<script>
import { listBsProject, getBsProject, delBsProject, addBsProject, updateBsProject } from "@/api/base/bsProject";
import { listDeptByType } from "@/api/system/dept";
export default {
  name: "BsProject",
  dicts: ["approval_status"],
  data () {
    return {
      isDisabled: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      dataList: [],
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectType: undefined,
        projectCatalog: undefined,
        projectAmout: undefined,
        projectName: undefined,
        deptId: undefined,
        deptName: undefined,
        projectLeader: undefined,
        planBeginData: undefined,
        planEndData: undefined,
        factBeginData: undefined,
        factEndData: undefined,
        projectStatus: undefined,
        approvalStatus: undefined,
        projectCode: undefined,
        projectGeneral: undefined,
        projectSacle: undefined,
        bidPrice: undefined,
        controlPrice: undefined,
        taxRate: undefined,
        shigongCompanyId: undefined,
        jianliCompanyId: undefined,
        shenjiCompanyId: undefined,
        address: undefined,
        postpone: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        projectType: [
          { required: true, message: "项目类型不能为空", trigger: "change" }
        ],
        projectCatalog: [
          { required: true, message: "项目分类不能为空", trigger: "blur" }
        ],
        projectAmout: [
          { required: true, message: "项目金额不能为空", trigger: "blur" }
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属单位id不能为空", trigger: "blur" }
        ],
        // deptName: [
        //   { required: true, message: "所属单位名称(冗余)不能为空", trigger: "blur" }
        // ],
        // projectLeader: [
        //   { required: true, message: "项目负责人不能为空", trigger: "blur" }
        // ],
        // planBeginData: [
        //   { required: true, message: "计划开始时间不能为空", trigger: "blur" }
        // ],
        // planEndData: [
        //   { required: true, message: "计划结束时间不能为空", trigger: "blur" }
        // ],
        // factBeginData: [
        //   { required: true, message: "实际开始时间不能为空", trigger: "blur" }
        // ],
        // factEndData: [
        //   { required: true, message: "实际开始时间不能为空", trigger: "blur" }
        // ],
        // projectStatus: [
        //   { required: true, message: "项目状态不能为空", trigger: "change" }
        // ],
        // approvalStatus: [
        //   { required: true, message: "项目审批状态不能为空", trigger: "change" }
        // ],
        // projectCode: [
        //   { required: true, message: "项目编码不能为空", trigger: "blur" }
        // ],
        // projectGeneral: [
        //   { required: true, message: "项目概况不能为空", trigger: "blur" }
        // ],
        // projectSacle: [
        //   { required: true, message: "项目规模不能为空", trigger: "blur" }
        // ],
        // bidPrice: [
        //   { required: true, message: "投标价不能为空", trigger: "blur" }
        // ],
        // controlPrice: [
        //   { required: true, message: "控制价不能为空", trigger: "blur" }
        // ],
        // taxRate: [
        //   { required: true, message: "税率不能为空", trigger: "blur" }
        // ],
        // shigongCompanyId: [
        //   { required: true, message: "施工单位id不能为空", trigger: "blur" }
        // ],
        // jianliCompanyId: [
        //   { required: true, message: "监理单位id不能为空", trigger: "blur" }
        // ],
        // shenjiCompanyId: [
        //   { required: true, message: "审计单位id不能为空", trigger: "blur" }
        // ],
        // address: [
        //   { required: true, message: "地区不能为空", trigger: "blur" }
        // ],
        // postpone: [
        //   { required: true, message: "是否延期 0否 1是不能为空", trigger: "blur" }
        // ],
        // delFlag: [
        //   { required: true, message: "不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created () {
    this.getList();
    this.getDeptList();
  },
  methods: {
    /** 查询项目列表 */
    getList () {
      this.loading = true;
      listBsProject(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取建设单位
    getDeptList () {
      const params = {
        deptType: 1
      };
      listDeptByType(params).then(response => {
        this.deptOptions = response.data;
      });
    },

    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        projectId: undefined,
        projectType: undefined,
        projectCatalog: undefined,
        projectAmout: undefined,
        projectName: undefined,
        deptId: undefined,
        deptName: undefined,
        projectLeader: undefined,
        planBeginData: undefined,
        planEndData: undefined,
        factBeginData: undefined,
        factEndData: undefined,
        projectStatus: undefined,
        approvalStatus: undefined,
        projectCode: undefined,
        projectGeneral: undefined,
        projectSacle: undefined,
        bidPrice: undefined,
        controlPrice: undefined,
        taxRate: undefined,
        shigongCompanyId: undefined,
        jianliCompanyId: undefined,
        shenjiCompanyId: undefined,
        address: undefined,
        postpone: undefined,
        delFlag: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
      this.isDisabled = false;
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.projectId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    setDeptName (value) {
      const info = this.deptOptions.find(item => item.deptId === value);
      this.form.deptName = info.deptName;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate (row, isDisabled) {
      this.loading = true;
      this.reset();
      const projectId = row.projectId || this.ids;
      this.isDisabled = isDisabled;
      getBsProject(projectId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.projectId != null) {
            updateBsProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBsProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const projectIds = row.projectId || this.ids;
      this.$modal.confirm('是否确认删除项目名称为"' + row.projectName + '"的数据项？').then(() => {
        this.loading = true;
        return delBsProject(projectIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /**审批操作 */
    onApproval (row, approvalStatus) {
      this.$modal.confirm('是否确认为"' + row.projectName + '"项目发起审批？').then(() => {
        return this.updateApprovalStatus(row, approvalStatus);
      });
    },
    // 修改审批状态
    updateApprovalStatus (row, approvalStatus) {
      const info = {
        approvalStatus: approvalStatus,
        projectId: row.projectId,
        projectName: row.projectName,
      };
      updateBsProject(info).then(response => {
        this.$modal.msgSuccess("操作成功");
      }).finally(() => {
        this.getList();
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('base/bsProject/export', {
        ...this.queryParams
      }, `bsProject_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
